import os
import time

import pinia
from effect.Form.FormComponts.NewSaveConfig import NewSaveConfig
from game.TopClass.windows import Windows
from game_control.save.saveManager import SaveManager


class NewGame(Windows):
    """
    开启一段新的游戏
    """

    def filter_callback(self, name, value):

        # region 存档名的默认值
        if name == "saveName" and value == "":
            value = str(time.time())
        # endregion

        # region 判断存档名是否重复
        if name == "saveName":
            try:
                os.mkdir(f"./json/Save/{value}")
            except Exception as e:
                print("输入了一个重复的存档名称 建议换一个")
                return False, None, None
        # endregion

        return True, name, value

    def UiShow(self):

        newSaveConfig = NewSaveConfig()  # 加载新存档
        _json: dict = newSaveConfig.collection(filter_callback=self.filter_callback)  # 填写后 把数据存放到json文件里面
        saveName = _json.get("saveName")

        saveManager = SaveManager()
        saveManager.newSave(_json)

        saveManager.loadSave(saveName)
        pinia.store['savemanager'] = saveManager
        pinia.store['savename'] = saveName
